<?php
class ModelService
{
	private $modelName;
	private $tableName;
	private $fieldNames;
	private $fieldTypes;
	private $displayNames;
	private $modelId;

	/**
	 * 新增模版数据
	 * @Author   Leikes
	 * @DateTime 2016-01-12T16:39:50+0800
	 */
	public function addModel()
	{
		try
		{
			DB::transaction(function()
			{
			    $model=new Model;
				$model->model_name=$this->modelName;
				$model->table_name=$this->tableName;
				$model->save();
				$modelOne=Model::where('table_name','=',$this->tableName)->first();
				$this->modelId=$modelOne->id;
				for($i=0;$i<count($this->fieldNames);$i++)
				{
					$field               =new Field;
					$field->model_id     =$this->modelId;
					$field->field_name   =$this->fieldNames[$i];
					$field->field_type   =$this->fieldTypes[$i];
					$field->display_name =$this->displayNames[$i];
					$field->save();
				}
				$this->createTable();
				$this->createModel();
			});

		}
		catch(Exception $e)
		{
			return CMSException::$addModelErr;
		}

		return CMSException::$ok;
	}

	/**
	 * 新增模版数据同时新增表格
	 * @Author   Leikes
	 * @DateTime 2016-01-12T16:56:57+0800
	 */
	public function createTable()
	{
		DB::transaction(function()
		{
		  	Schema::create($this->tableName,function($table)
			{
				$table->increments('id');
				for($i=0;$i<count($this->fieldNames);$i++)
				{
					$fieldType=$this->fieldTypes[$i];
					$table->$fieldType($this->fieldNames[$i]);
				}
				$table->integer('category_id');
				$table->timestamps();
			});
		});
	}

	/**
	 * 创建框架model文件
	 * @Author   Leikes
	 * @DateTime 2016-01-12T17:08:48+0800
	 */
	public function createModel()
	{
		File::put(app_path().'/models/'.ucfirst($this->tableName).'.php','<?php'."\n".'class '.ucfirst($this->tableName)." extends \Eloquent\n".'{	protected $table="'.$this->tableName.'";	} ?>');
	}


	/**
	 * 删除模型
	 * @Author   Leikes
	 * @DateTime 2016-01-13T10:57:45+0800
	 * @param    integer                   $modelId 模型id
	 * @return   array                            errCode
	 */
	public function deleteModel()
	{	
		try
		{
			DB::transaction(function()
			{
			    $fields =Field::where('model_id','=',$this->modelId)->delete();
				$model  =Model::find($this->modelId);
				File::delete('../app/models/'.$model->table_name.'.php');
				$model->delete();
				Schema::drop($model->table_name);
			});

		}
		catch(Exception $e)
		{
			return CMSException::$deleteModelErr;
		}
		return CMSException::$ok;
	}

	/**
	 * 更新模型
	 * @Author   Leikes
	 * @DateTime 2016-01-13T11:17:45+0800
	 * @return   array                   errCode
	 */
	public function updateModel()
	{
		$validator = Validator::make(
		    array('modelId' => $this->modelId),
		    array('modelId' => 'required|exists:model,id' )
		);
	
		if($validator->fails())
		{
			return Response::json( CMSException::$updateModelErr);
		}
		try
		{
			DB::transaction(function()
			{
				$model             =Model::find($this->modelId);
				$model->model_name =$this->modelName;
				$model->save();
			});

		}
		catch(Exception $e)
		{
			return CMSException::$updateModelErr;
		}

		return CMSException::$ok;
	}

	/**
	 * 查看全部模板数据
	 * @Author   Leikes
	 * @DateTime 2016-01-13T14:03:52+0800
	 * @return   object                   模型对象
	 */
	public function findAll()
	{
		$models=Model::all();
		return $models;
	}

	/**
	 * 根据id查询模版的数据
	 * @Author   Leikes
	 * @DateTime 2016-01-13T14:12:25+0800
	 * @param    integer                   $modelId 模板id
	 * @return   object                            model对象
	 */
	public function findById()
	{
		$validator = Validator::make(
		    array('modelId' => $this->modelId),
		    array('modelId' => 'required|exists:model,id' )
		);
	
		if($validator->fails())
		{
			return CMSException::$findModelErr;
		}

		$model=Model::find($this->modelId);
		
		return $model;
	}

	public function setModelName($modelName)
	{
		$this->modelName    =$modelName;
	}
	
	public function setTableName($tableName)
	{
		$this->tableName    =$tableName;
	}
	
	public function setFieldNames($fieldNames)
	{
		$this->fieldNames   =$fieldNames;
	}
	
	public function setFieldTypes($fieldTypes)
	{
		$this->fieldTypes   =$fieldTypes;
	}
	
	public function setDisplayNames($displayNames)
	{
		$this->displayNames =$displayNames;
	}

	public function setModelId($modelId)
	{
		$this->modelId=$modelId;
	}
}
?>
